package com.xiaoqianxin.api.controller;

import com.xiaoqianxin.api.service.TeamService;
import com.xiaoqianxin.entity.CommonResult;
import com.xiaoqianxin.entity.Team;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 队伍
 */
@RestController
@RequestMapping("/teams")
public class TeamController {

    private static Logger LOG = LoggerFactory.getLogger(TeamController.class);

    @Autowired
    private TeamService service;

    @RequestMapping(value = "/getTeams")
    public CommonResult query(@RequestParam(value = "id", required = false) String id,
                              @RequestParam(value = "type", required = false) String type,
                              @RequestParam(value = "name", required = false) String name,
                              @RequestParam(defaultValue = "0") int pageNum,
                              @RequestParam(defaultValue = "10") int pageSize) {
        LOG.info("查询队伍,id={},type={},name={}", id, type, name);
        PageRequest pageable = PageRequest.of(pageNum, pageSize);
        Page<Team> page = service.query(id, type, name, pageable);
        CommonResult result = new CommonResult();
        if (page != null) {
            List<Team> content = page.getContent();
            if (content.size() > 0) {
                result.setSucceed(content);
            } else {
                result.setFailed("没有数据");
            }
        } else {
            result.setFailed("没有数据");
        }
        LOG.info("查询队伍,结果={}", page);
        return result;
    }



    @RequestMapping(value = "/test")
    public CommonResult test() {
        CommonResult result = new CommonResult();
        result.setSucceed("最新一次测试！");
        return result;
    }


}
